using System.IO.IsolatedStorage;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;

namespace FU.Capstones.IMS.Web.Common
{
    public static class IMSIsolatedStorage
    {
        private static readonly IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings;

        #region CurrentUser

        private const string CurrentUserKey = "CurrentUser";
        public static Account CurrentUser
        {
            get
            {
                Account account;
                if (Settings.TryGetValue(CurrentUserKey, out account))
                    return account;
                return new Account();
            } 
            set
            {
                if (Settings.Contains(CurrentUserKey))
                {
                    Settings[CurrentUserKey] = value;
                }
                else
                {
                    Settings.Add(CurrentUserKey, value);
                }
                
                Settings.Save();
            }
        }

        #endregion

        #region RememberPassword

        private const string RememberPasswordKey = "RememberPassword";
        public static bool RememberPassword
        {
            get
            {
                bool rememberPassword;
                if (Settings.TryGetValue(RememberPasswordKey, out rememberPassword))
                {
                    return rememberPassword;
                }
                return false;
            }
            set
            {
                if (Settings.Contains(RememberPasswordKey))
                {
                    Settings[RememberPasswordKey] = value;
                }
                else
                {
                    Settings.Add(RememberPasswordKey, value);
                }
                Settings.Save();
            }
        }

        #endregion

        #region Clear Data

        public static void Clear()
        {
            if (RememberPassword)
            {
                Account temp = new Account() {Username = CurrentUser.Username, Password = CurrentUser.Password};
                Settings.Clear();
                CurrentUser = temp;
                RememberPassword = true;
                Settings.Save();
            }
            else
            {
                Settings.Clear();
            }
        }

        #endregion

    }
}